package cn.tedu.erp.project.service.impl;

import cn.tedu.erp.project.ex.ServiceException;
import cn.tedu.erp.project.mapper.CustomAgreementMapper;
import cn.tedu.erp.project.pojo.dto.CustomAgreementAddNewDTO;

import cn.tedu.erp.project.pojo.dto.CustomAgreementUpdateDTO;
import cn.tedu.erp.project.pojo.dto.WareHouseCategoryUpdateDTO;
import cn.tedu.erp.project.pojo.entity.CustomAgreement;
import cn.tedu.erp.project.pojo.entity.CustomManage;
import cn.tedu.erp.project.pojo.entity.WareHouseCategory;
import cn.tedu.erp.project.pojo.vo.*;
import cn.tedu.erp.project.service.ICustomAgreementService;
import cn.tedu.erp.project.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public  class CustomAgreementServiceImpl implements ICustomAgreementService {
    @Autowired
    private CustomAgreementMapper customAgreementMapper;
    @Override
    public void addNew(CustomAgreementAddNewDTO customAgreementAddNewDTO) {
        log.debug("开始处理【添加合同】的业务,参数: {}",customAgreementAddNewDTO);
        // 调用参数对象的getName()得到尝试添加的合同的名称
        String name = customAgreementAddNewDTO.getName();
        // 调用Mapper对象的countByName()执行统计查询
        int count = customAgreementMapper.countById(customAgreementAddNewDTO.getId());
        log.debug("根据合同id统计数量:{}",count);
        // 创建订单对象
        CustomAgreement customAgreement = new CustomAgreement();
        // 调用BeanUtils.copyProperties(源,目标)将参数对象中的属性复制到customSales对象中
        BeanUtils.copyProperties(customAgreementAddNewDTO,customAgreement);
        // 调用Mapper对象的insert()执行插入合同数据
        log.debug("插入数据:{}",customAgreement);
        customAgreementMapper.insert(customAgreement);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除合同】的业务，参数：{}", id);
        // 调用Mapper对象的getStandardById()执行查询
        CustomAgreementStandardVO customAgreement=customAgreementMapper.getStandardById(id);
        log.debug("根据ID={}检查合同数据是否存在，查询结果：{}", id, customAgreement);
        // 判断查询结果是否为null
        if (customAgreement == null) {
            // 是：数据不存在，抛出异常
            String message = "删除合同失败，尝试删除的合同数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除，参数：{}", id);
        customAgreementMapper.deleteById(id);
    }
    @Override
    public void updateInfoById(Long id, CustomAgreementUpdateDTO customAgreementUpdateDTO) {
        log.debug("开始处理【修改商品详情】的业务，ID：{}，新数据：{}", id, customAgreementUpdateDTO);
        // 调用Mapper对象的getStandardById()执行查询
        CustomAgreementStandardVO queryResult = customAgreementMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：抛出异常
            String message = "修改商品详情失败，尝试修改的商品数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 创建WareHouseCategory对象
        CustomAgreement customAgreement = new CustomAgreement();
        // 复制属性，设置ID
        BeanUtils.copyProperties(customAgreementUpdateDTO, customAgreement);
        customAgreement.setId(id);
        // 调用Mapper对象的update()方法执行修改
        customAgreementMapper.update(customAgreement);
    }

    @Override
    public CustomAgreementStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询合同详情】的业务，参数：{}", id);
        // CustomAgreementStandardVO queryResult = mapper.getStandardById(id);
        CustomAgreementStandardVO customAgreement= customAgreementMapper.getStandardById(id);
        if (customAgreement == null) {
            // 是：抛出异常
            String message = "查询合同详情失败，合同数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return customAgreement;
    }
    @Override
    public List<CustomAgreementListVO> list() {
        log.debug("开始处理【列表】的业务,无参数");
        List<CustomAgreementListVO> list = customAgreementMapper.list();
        return list;
    }

    @Override
    public void rebuildCache() {

    }
}
